﻿using Ldt.DataEngine.DataFlowParse.Model;
using Ldt.DataEngine.RuleAssemble;
using LdtDataEngin.ModuleDetail.BaseModule;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.StringAssemble;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.LawElementModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.CommonModule.LawDeduce
{
    public class QutoLawdeDuction : AssermBase
    {
        /// <summary>
        /// 分析数据集合
        /// </summary>
        public List<DataModel> dataModelList = new List<DataModel>();

        /// <summary>
        /// xml帮助类
        /// </summary>
        private XmlUtilCommon xmlUtilCommon;

        /// <summary>
        /// 法律法规引用类
        /// </summary>
        /// <param name="dataContext">上下文</param>
        /// <param name="moduleName">模块名称</param>
        public QutoLawdeDuction(DataContext dataContext, string moduleName)
        {
            this.dataContext = dataContext;
            MoudleName = moduleName;
        }

        /// <summary>
        /// 法律法规推导
        /// </summary>
        public void LawDuction()
        {
            try
            {
                xmlUtilCommon = new XmlUtilCommon() { dataContext = dataContext };
                //获取模块配置
                var configModel = dataContext.DataLayerSys.GetConfigModel(MoudleName);
                LawLocateDetail lawLocateDetail = null;
                if (configModel != null)
                {
                    //获取法条定位配置
                    ConfigModel lawLocateConfig = configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower() == "lawlocate");
                    if (lawLocateConfig != null && !string.IsNullOrWhiteSpace(lawLocateConfig.ConfigContent))
                    {
                        LawLocateConfigModel lawlocateModel = XmlUtility.DeserializeToObject<LawLocateConfigModel>(lawLocateConfig.ConfigContent);
                        var lawCaseType = lawlocateModel.lawCaseTypeList.FirstOrDefault(x => x.code == dataContext.DataLayerAns.CaseType);
                        if (lawCaseType != null)
                            lawLocateDetail = lawCaseType.lawLocateDetail;
                    }
                }
                bool isopt = true;
                if (!string.IsNullOrWhiteSpace(lawLocateDetail?.xmlPath))
                {
                    dataModelList = dataContext.DataLayerOpt.SourdatModleList;

                    if (dataModelList==null || dataModelList.Count == 0)
                    {
                        if (dataContext.DataLayerAns.AnalysisResultsDataModel != null && dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList != null && dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList.Count > 0)
                        {
                            isopt = false;
                            dataModelList = dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList;
                        }
                    }
                    List<DataModel> dataModelRootList = XmlUtility.ConditionDataModelList(dataModelList, "", lawLocateDetail.xmlPath);
                    if (dataModelRootList != null && dataModelRootList.Count > 0)
                    {
                        List<DataModel> lawRootModelList = dataModelRootList;
                        foreach (DataModel sentenceModel in lawRootModelList)
                        {
                            List<DataModel> sentenceModelList = xmlUtilCommon.SubordinateDataModelList(dataModelList, sentenceModel);
                            Law(sentenceModelList, sentenceModel);
                        }
                    }
                }
                if (isopt)
                {
                    XmlDocument xmlDocument = XmlUtility.DataModelByXml(dataContext.DataLayerOpt.SourdatModleList);
                    dataContext.DataLayerOpt.OptimizationXml = xmlDocument;
                }
                else
                {
                    XmlDocument xmlDocument = XmlUtility.DataModelByXml(dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList);
                    dataContext.DataLayerAns.AnalysisResultsXml = xmlDocument;
                }

            }
            catch (Exception ex)
            {
                dataContext.InteractiveShow.LogShow("法律法规转ID错误:" + ex.ToString(), LogMessageType.Error);
            }
        }

        /// <summary>
        /// 查询法规
        /// </summary>
        /// <param name="lawModelList">法条内容</param>
        /// <param name="lawModelList">法条节点</param>
        public void Law(List<DataModel> lawModelList, DataModel lawDataModel)
        {
            if (lawModelList.FirstOrDefault(a => a.ParentId == lawDataModel.ID) != null)
            {
                foreach (DataModel lawDataP in lawModelList.Where(a => a.ParentId == lawDataModel.ID).ToList())
                {
                    if (lawModelList.FirstOrDefault(a => a.ParentId == lawDataP.ID) != null)
                    {
                        foreach (DataModel lawData in lawModelList.Where(a => a.ParentId == lawDataP.ID).ToList())
                        {
                            if (lawData.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "引用法规id") != null)
                            {
                                string lawname = lawData.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "引用法规id").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                                //查询获取相应的法规ID；
                                string LawLibraryCode = string.Empty;
                                lawname = lawname.Replace("《", "").Replace("》", "");
                                var lawG = QuoteLawExt.GetLawIDTupleModel(lawname, out LawLibraryCode);
                                if (!string.IsNullOrWhiteSpace(lawG.Item1))
                                {
                                    lawData.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "引用法规id").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue = lawG.Item1;
                                }

                                //法条ID
                                if (dataModelList.FirstOrDefault(a => a.ParentId == lawData.ID) != null && dataModelList.FirstOrDefault(a => a.ParentId == dataModelList.FirstOrDefault(b => b.ParentId == lawData.ID).ID) != null)
                                {
                                    foreach (DataModel provData in dataModelList.Where(a => a.ParentId == dataModelList.FirstOrDefault(b => b.ParentId == lawData.ID).ID).ToList())
                                    {
                                        if (provData.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "引用条id") != null)
                                        {
                                            string provname = provData.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "引用条id").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                                            Match tempMatchtiao = Regex.Match(provname, "第.+?条之([一二三四五六七八九十零]+|[0-9]+)");
                                            int provNumbzhi = Convert.ToInt32(LdFormat.DecodeNumberCn(tempMatchtiao.Groups[1].Value));
                                            string tempReg = RuleParseClass.GerRegValue("QuoteLawExtractA4");
                                            Match tempMatch = Regex.Match(provname, tempReg);
                                            string provChineseNumb = (tempMatch == null) ? "" : tempMatch.Groups[1].Value;
                                            int provNumb = Convert.ToInt32(LdFormat.DecodeNumberCn(provChineseNumb));
                                            string provID = QuoteLawExt.GetProvID(lawG.Item1, LawLibraryCode, provNumb, provNumbzhi);
                                            if (!string.IsNullOrWhiteSpace(provID))
                                            {
                                                provData.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "引用条id").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue = provID;
                                            }
                                            if (dataModelList.FirstOrDefault(a => a.ParentId == provData.ID) != null && dataModelList.FirstOrDefault(a => a.ParentId == dataModelList.FirstOrDefault(b => b.ParentId == provData.ID).ID) != null)
                                            {
                                                foreach (DataModel clauseData in dataModelList.Where(a => a.ParentId == dataModelList.FirstOrDefault(b => b.ParentId == provData.ID).ID).ToList())
                                                {
                                                    if (clauseData.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "款排序") != null)
                                                    {
                                                        string singleQuoteprov = clauseData.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "款排序").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                                                        string MReg = RuleParseClass.GerRegValue("QuoteLawExtractA6");
                                                        tempMatch = Regex.Match(singleQuoteprov, MReg);
                                                        string clauseName = (tempMatch == null) ? "" : tempMatch.Value;
                                                        string ClauseChineseNumb = (tempMatch == null) ? "" : tempMatch.Groups[1].Value;
                                                        int ClauseNumb = Convert.ToInt32(LdFormat.DecodeNumberCn(ClauseChineseNumb));    //款序号
                                                        if (ClauseNumb != 0)
                                                        {
                                                            clauseData.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "款排序").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue = ClauseNumb.ToString();
                                                        }

                                                    }
                                                    if (dataModelList.FirstOrDefault(a => a.ParentId == clauseData.ID) != null && dataModelList.FirstOrDefault(a => a.ParentId == dataModelList.FirstOrDefault(b => b.ParentId == clauseData.ID).ID) != null)
                                                    {
                                                        foreach (DataModel itemData in dataModelList.Where(a => a.ParentId == dataModelList.FirstOrDefault(b => b.ParentId == clauseData.ID).ID).ToList())
                                                        {
                                                            string singleQuoteprov = itemData.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "项排序").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                                                            string TMReg = RuleParseClass.GerRegValue("QuoteLawExtractA8");
                                                            tempMatch = Regex.Match(singleQuoteprov, TMReg);
                                                            string ItemName = (tempMatch == null) ? "" : tempMatch.Value;
                                                            string ItemChineseNumb = (tempMatch == null) ? "" : tempMatch.Groups[1].Value;
                                                            int ItemNumb = Convert.ToInt32(LdFormat.DecodeNumberCn(ItemChineseNumb));        //项序号
                                                            if (ItemNumb != 0)
                                                            {
                                                                itemData.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "项排序").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue = ItemNumb.ToString();
                                                            }

                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
